#include "lock.h"
#include "kfunc.h"

// void
// spin_lock (struct spinlk * spnlk) {
//     // NOTE: 虚拟机未实现A拓展,暂时使用组合指令代替,有极低概率锁失效
//     while (spnlk->lk); 
//     spnlk->lk = 1;
// }

// void
// spin_unlock (struct spinlk * spnlk) {
//     spnlk->lk = 0;
// }

int lockready = 0;

void
lockinit () {
    lockready = 1;
}

void
sleep_lock (struct sleeplk * slplk) {
    if (!lockready) return;
    while (slplk->lk)
        sleep (slplk);
        // NOTE: 虚拟机未实现A拓展,有极低概率锁失效
    slplk->lk = 1;
}

void
sleep_unlock (struct sleeplk * slplk) {
    if (!lockready) return;
    slplk->lk = 0;
    wakeup (slplk);
}